package com.example.myapplication.slice;

import com.example.myapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.TextField;
import ohos.agp.window.dialog.ToastDialog;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class LoginAbilitySlice extends AbilitySlice implements Component.ClickedListener {

    private static final HiLogLabel LABEL_LOG = new HiLogLabel(0, 0, "MainAbility");
    private Button btnlogin;
    private TextField account, password;
    public static final int REQUEST_CODE_MAINABLITITY = 1;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        btnlogin = findComponentById(ResourceTable.Id_button);
        account = findComponentById(ResourceTable.Id_account);
        password = findComponentById(ResourceTable.Id_password);
        btnlogin.setClickedListener(this);
//        btnlogin.setClickedListener(component -> {
//            HiLog.info(LABEL_LOG, "click");
//            new ToastDialog(MainAbilitySlice.this).setText("点击了登录按钮").setDuration(2000).show();
//            intent.setParam("account", "杨子浩");
//            intent.setParam("password", "123456");
//            present(new LoginAbilitySlice(), intent);
//            presentForResult(new LoginAbilitySlice(), intent, 1);
//        });
    }

    @Override
    public void onClick(Component component){
        HiLog.info(LABEL_LOG, "click");
        new ToastDialog(this).setText("点击了登录按钮").setDuration(2000).show();
        //无参数跳转
        //present(new MainAbilitySlice(), new Intent());
        //有参数跳转
        Intent intent = new Intent();
        intent.setParam("account", "杨子浩");
//        present(new MainAbilitySlice(), intent);
        presentForResult(new MainAbilitySlice(), intent, REQUEST_CODE_MAINABLITITY);
    }
    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onResult(int requestCode, Intent resultIntent) {
        super.onResult(requestCode, resultIntent);
        if (requestCode == REQUEST_CODE_MAINABLITITY) {
            new ToastDialog(this).setText(resultIntent.getStringParam("backText")).setDuration(2000).show();
        }
    }
}